package com.mxwl.cbg.common.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class EquipmentConfig {
    private static Map<String, EquipmentDesc> equipment_info = new HashMap<>();
    private static Map<String, Integer> equipment_kindid_map = new HashMap<>();
    private static final Object lock = new Object();
    private static boolean isLoaded = false;
    private static void loadConfig() {
        synchronized (lock) {
            if (!isLoaded) {
                try (InputStream inputStream = EquipmentConfig.class.getClassLoader().getResourceAsStream("equipment_config.json")) {
                    if (inputStream == null) {
                        throw new RuntimeException("Unable to find equipment_config.json in classpath.");
                    }

                    // 将 InputStream 转换为字符串
                    String jsonContent = new java.util.Scanner(inputStream, "UTF-8").useDelimiter("\\A").next();

                    // 使用 FastJSON 反序列化为 Map<String, EquipmentDesc>
                    Map<String, EquipmentDesc> config = JSON.parseObject(
                            jsonContent,
                            new TypeReference<Map<String, EquipmentDesc>>() {}
                    );

                    if (config == null || config.isEmpty()) {
                        throw new RuntimeException("Failed to parse equipment_config.json: empty or invalid content.");
                    }

                    equipment_info = config;

                    // 构建 name -> kind_id 映射
                    for (String kindId : equipment_info.keySet()) {
                        EquipmentDesc desc = equipment_info.get(kindId);
                        if (desc != null && desc.getName() != null) {
                            equipment_kindid_map.put(desc.getName(), Integer.parseInt(kindId));
                        }
                    }

                    isLoaded = true;
                } catch (Exception e) {
                    throw new RuntimeException("Failed to load equipment_config.json", e);
                }
            }
        }
    }



    private static int getInt(Map<String, Object> map, String key) {
        Object val = map.get(key);
        if (val == null) {
            throw new RuntimeException("Field '" + key + "' is missing in config.");
        }
        if (val instanceof Integer) {
            return (Integer) val;
        } else {
            throw new RuntimeException("Field '" + key + "' must be an integer.");
        }
    }

    /**
     * 判断是否是武器
     *
     * @param eqiup_id 装备的id
     * @return
     */
    public static boolean isWeapon(int eqiup_id) {
        Map<String, EquipmentDesc> equipmentDescMap = geEquipmentInfo();
        if (equipmentDescMap.containsKey(String.valueOf(eqiup_id))) {
            EquipmentDesc equipmentDesc = equipmentDescMap.get(String.valueOf(eqiup_id));
            return equipmentDesc.getKind_name().equals("武器");
        }
        return false;
    }

    /**
     * 判断是否是武器
     *
     * @param name 装备名称
     * @return
     */
    public static boolean isWeapon(String name) {
        Map<String, EquipmentDesc> equipmentDescMap = geEquipmentInfo();
        for (String equip_id : equipmentDescMap.keySet()) {
            if (equipmentDescMap.get(equip_id).getName().equals(name)){
                return equipmentDescMap.get(equip_id).getKind_name().equals("武器");
            }
        }
        return false;
    }

    /**
     * 判断是否是装备
     *
     * @param id
     * @return
     */
    public static boolean isEquipment(String name) {
        if (!isLoaded) {
            loadConfig();
        }
        return equipment_kindid_map.containsKey(name);
    }

    /**
     * 根据装备名称获取装备的类别id
     *
     * @param name 装备名称
     * @return
     */
    public static int getEquipmentKindid(String name) {
        if (!isLoaded) {
            loadConfig();
        }
        return equipment_kindid_map.get(name);
    }

    /**
     * 根据装备id获取装备的类别id
     *
     * @param id
     * @return
     */
    public static int getEquipmentKindid(int id) {
        if (!isLoaded) {
            loadConfig();
        }
        if (!equipment_info.containsKey(String.valueOf(id))) {
            return -1;
        }
        return equipment_info.get(String.valueOf(id)).getKind_id();
    }

    /**
     * 判断是否是项链
     *
     * @param id
     * @return
     */
    public static Boolean itNecklace(int id) {
        if (!isLoaded) {
            loadConfig();
        }
        if (equipment_info.containsKey(String.valueOf(id))) {
            return equipment_info.get(String.valueOf(id)).getKind_id() == 21;
        }
        return false;
    }

    public static Map<String, EquipmentDesc> geEquipmentInfo() {
        if (!isLoaded) {
            loadConfig();
        }
        return equipment_info;
    }

    @Data
    public static class EquipmentDesc {
        private Integer id;
        private String name;
        private String kind_name;
        private Integer kind_id;
        private Integer min_level;
        private Integer max_level;
        private Boolean reinforced;
        private String type;
        private String desc;
    }

    public static void main(String[] args) {
        System.out.println(geEquipmentInfo());
    }
}
